<script>
	const httpRequest = require("@/common/httpRequest.js")
	import WebSocket from '@/common/websocket.js' //websocket封装
	// import {
	// 	igexinTool
	// } from '@/utils/unipushbm.js'
	export default {
		data() {
			return {
				websocket: null
			}
		},
		onLaunch: function() {
			// 1 判断手机权限 , 如果没有权限就去设置
			//#ifdef APP-PLUS
			checkNotificationAuthorized();
			//#endif
			httpRequest.initApp();
			let that = this
			// 2.启动监听推送消息事件
			uni.onPushMessage((res) => {
				console.log(">>>>", res);
				// 监听通知栏消息的点击  
				if (res.type == 'click') {
					// 如果需要跳转app内指定页面，则自己实现下方的跳转代码。  
					if (httpRequest.checkIsLogin()) {
						let data = {
							firend_id: res.data.payload.firend_id,
							friend_name: res.data.payload.friend_name,
							firend_avatar_text: res.data.payload.firend_avatar_text,
						}
						uni.navigateTo({
							url: "/pages/chat/chat?msg=" + encodeURIComponent(JSON.stringify(data))
						})
					} else {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
				}
				// 监听在线推送消息，若云函数设置了 "force_notification":true，则不会触发此 receive。  
				if (res.type == 'receive') {
					console.log("接收到的消息内容", res.payload);
				}
			})
		},
		onShow: function() {
			console.log('App Show')
			if (httpRequest.checkIsLogin()) { //已经登陆,创建websocket连接
				this.websocketStart()
				this.getChatNum()
			} else {
				this.clearBadge()
			}
			//#ifdef APP-PLUS
			// 获取本地应用资源版本号
			plus.runtime.getProperty(plus.runtime.appid, (inf) => {
				console.error(uni.getSystemInfoSync().platform, inf.versionCode);
				//获取服务器的版本号
				httpRequest.request('/api/index/getDownloadUrl', 'POST', {
					version_type: uni.getSystemInfoSync().platform, //android或者ios
					edition_number: inf.versionCode, // 打包时manifest设置的版本号 
					app_type: 2,
				}).then(res => {
					console.log("版本更新", res.data);
					//判断后台返回版本号是否大于当前应用版本号 && 是否发行 （上架应用市场时一定不能弹出更新提示）
					if (Number(res.data.edition_number) > Number(inf.versionCode) && res
						.data.data.edition_issue == 1) {
						//如果是wgt升级，并且是静默更新 （注意！！！ 如果是手动检查新版本，就不用判断静默更新，请直接跳转更新页，不然点击检查新版本后会没反应）
						if (res.data.package_type == 1 && res.data.edition_silence ==
							1) {
							//调用静默更新方法 传入下载地址
							silenceUpdate(res.data.edition_url)
						} else {
							//跳转更新页面 （注意！！！如果pages.json第一页的代码里有一打开就跳转其他页面的操作，下面这行代码最好写在setTimeout里面设置延时3到5秒再执行）
							setTimeout(()=>{
								uni.navigateTo({
									url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
										JSON.stringify(res.data)
								});
							},5000)
						}
					} else {
						// 如果是手动检查新版本 需开启以下注释
						/* uni.showModal({
						    title: '提示',
						    content: '已是最新版本',
						    showCancel: false
						}) */
					}
				})
			});
			//#endif
		},
		onHide: function() {
			console.log('App Hide')
			this.websocket.close()
			if (httpRequest.checkIsLogin()) { //已经登陆,创建websocket连接
				this.getChatNum()
			}else{
				this.clearBadge()
			}
		},
		methods: {
			getChatNum(){
				httpRequest.request('/api/user/getChatNum', 'GET', {
					ly:2//1=用户端   2=商家端
				}).then(res => {
					if(res.code==1){
						this.setBadge(res.data.total_noread_num)
					}
				})
			},
			//设置角标
			setBadge(num) {
				// #ifdef APP-PLUS
				plus.runtime.setBadgeNumber(num)
				// #endif
			},
			//清除角标
			clearBadge() {
				// #ifdef APP-PLUS
				plus.runtime.setBadgeNumber(-1)
				// #endif
			},
			websocketStart() {
				this.websocket = new WebSocket("wss://chat.hongniaotianxuan.com:8282");
				this.websocket.connect()
				this.websocket.onMessage(result => {
					console.log('app.js收到服务器内容：' + result.data);
					uni.$emit('updatechatFnte', "")
					uni.$emit('updatechatFnteList', "")
				})
			}
		}
	}
	// 检查app是否开启了通知权限 安卓苹果通用
	function checkNotificationAuthorized() {
		const notificationAuthorized = uni.getAppAuthorizeSetting().notificationAuthorized
		if (notificationAuthorized !== 'authorized') {
			uni.showModal({
				title: '通知权限',
				content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
				confirmText: '去设置',
				showCancel: false,
				success: (res) => {
					if (res.confirm) uni.openAppAuthorizeSetting()
				}
			});
		}
	}
</script>

<style lang="scss">
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "uview-ui/index.scss";


	.placeholderBox {
		width: 406rpx;
		height: 406rpx;
	}

	.placeholderBoxTop {
		margin-top: 100rpx;
	}

	.xuanzhongs {
		background-image: url("static/image/home/xuanzhong.png");
		width: 40rpx;
		height: 40rpx;
		background-size: 100% 100%;
	}

	.wiexuanzhongs {
		background-image: url("static/image/home/wiexuanzhong.png");
		width: 40rpx;
		height: 40rpx;
		background-size: 100% 100%;
	}

	.right_icon1 {
		width: 20.35rpx;
		height: 25.35rpx;
		background-image: url('static/image/mine/right_icon.png');
		background-size: 100% 100%;
	}

	.footer {
		width: 100%;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;

		.footerButton {
			padding: 20rpx;
			border-radius: 41rpx;
			text-align: center;
			background-color: rgba(243, 39, 28, 1);
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}

	.deletimg {
		background-image: url("@/static/image/home/delete.png");
		width: 50rpx;
		height: 30rpx;
		position: absolute;
		right: 0;
		background-size: 100%, 100%;
		top: 0;
	}

	.positionr {
		position: relative;
	}

	.flex1 {
		flex: 1;
	}

	.width0 {
		width: 0;
	}

	.flex {
		display: flex;
	}

	.flextop {
		display: flex;
		align-items: flex-start;
		justify-content: center;
	}

	.flextops {
		display: flex;
		align-items: flex-start;
	}

	.flexcenter {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.flexcolumn {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.flexbottom {
		display: flex;
		align-items: flex-end;
		justify-content: center;
	}

	.flexbetween {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flexleft {
		display: flex;
		align-items: center;
	}

	.smalltext {
		font-size: 20rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
	}

	.xiaohong {
		color: #ff0000;
	}

	.flexright {
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.flexaround {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.flexstart {
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
	}

	.text {
		text-align: center;
	}

	.textleft {
		text-align: left;
	}

	.textright {
		text-align: right;
	}

	.justify_between {
		display: flex;
		justify-content: space-between;
	}

	.flex_wrap {
		flex-wrap: wrap;
	}

	.content {
		background-color: #FAFAFA;
		// background-color: blue;
		min-height: calc(100vh);
		box-sizing: border-box;
		// overflow: hidden;
	}

	.ffffff {
		background-color: #ffffff;
	}

	.fafafa {
		background-color: #FAFAFA;
	}

	.f7f7f7 {
		background-color: #F7F7F7;
	}

	.footer_btn {
		font-size: 32rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 20rpx 20rpx;
		background-color: RGBA(255, 78, 66, 1);
		border-radius: 64rpx;
	}

	.footer_di {
		width: 100%;
		padding: 30rpx 30rpx 30rpx 30rpx;
		background-color: #ffff;
		position: fixed;
		bottom: 0rpx;
		box-sizing: border-box;
	}

	.color {
		color: #ffff;
	}

	.color_red {
		color: #FF0000;
	}

	.reds {
		color: #FF4E42;
	}

	.line {
		border-bottom: 1rpx RGBA(242, 242, 242, 1) solid;
	}

	.line_t {
		border-top: 1rpx RGBA(242, 242, 242, 1) solid;
	}

	.inp {
		text-align: right;
		color: #000000;

	}

	/* margin*/
	.margin {
		margin: 30rpx 30rpx;
	}

	.margin_top {
		margin-top: 30rpx;
	}

	.margin_top2 {
		margin-top: 20rpx;
	}

	.margin_top1 {
		margin-top: 10rpx;
	}

	.margin_top4 {
		margin-top: 40rpx;
	}

	.margin_top5 {
		margin-top: 50rpx;
	}

	.margin_top6 {
		margin-top: 60rpx;
	}

	.margin_top7 {
		margin-top: 70rpx;
	}

	.margin_left1 {
		margin-left: 10rpx;
	}

	.margin_left2 {
		margin-left: 20rpx;
	}

	.margin_left3 {
		margin-left: 30rpx;
	}

	.margin_left4 {
		margin-left: 40rpx;
	}

	.margin_left5 {
		margin-left: 50rpx;
	}

	.margin_left6 {
		margin-left: 60rpx;
	}

	.margin_left7 {
		margin-left: 70rpx;
	}

	.margin_right1 {
		margin-right: 10rpx;
	}

	.margin_right2 {
		margin-right: 20rpx;
	}

	.margin_right3 {
		margin-right: 30rpx;
	}

	.margin_right4 {
		margin-right: 40rpx;
	}

	.margin_right5 {
		margin-right: 50rpx;
	}

	.margin_right6 {
		margin-right: 60rpx;
	}

	.margin_right7 {
		margin-right: 70rpx;
	}





	.margin_bottom1 {
		margin-bottom: 10rpx;
	}

	.margin_bottom2 {
		margin-bottom: 20rpx;
	}

	.margin_bottom3 {
		margin-bottom: 30rpx;
	}

	.margin_bottom4 {
		margin-bottom: 40rpx;
	}

	.margin_bottom5 {
		margin-bottom: 50rpx;
	}

	.margin_bottom6 {
		margin-bottom: 60rpx;
	}


	/*  padding*/
	.padding {
		padding: 30rpx 30rpx;
	}

	.paddinglr {
		padding: 0 30rpx;
	}

	.paddingtb {
		padding: 30rpx 0;
	}

	.padding15 {
		padding: 15rpx;
	}

	.padding_tb {
		padding-bottom: 30rpx;
		padding-top: 30rpx;
	}

	.padding_top {
		padding-top: 30rpx;
	}

	.padding_top2 {
		padding-top: 250rpx;
	}

	.padding_top1 {
		padding-top: 10rpx;
	}

	.padding_top15 {
		padding-top: 15rpx;
	}

	.padding_left3 {
		padding-left: 30rpx;
	}

	.padding_left2 {
		padding-left: 20rpx;
	}

	.padding_left1 {
		padding-left: 10rpx;
	}

	.padding_right3 {
		padding-left: 30rpx;
	}

	.padding_right2 {
		padding-left: 20rpx;
	}

	.padding_right1 {
		margin-left: 10rpx;
	}

	.padding2 {
		padding: 26rpx;
	}

	.padding_lr {
		padding-left: 40rpx;
		padding-right: 40rpx;
	}

	.padding_bottom {
		padding-bottom: 30rpx;
	}

	.padding_lrb {
		padding: 0rpx 30rpx 30rpx 30rpx;
	}

	.white_nowrap {
		white-space: nowrap;
	}

	.word_wrap {
		word-wrap: break-word;
		word-break: break-all;
	}

	//超出一行省略号
	.yihang {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	//超出两行省略号
	.lianghang {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	/* 字体 */
	.text_a {
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_g {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_b {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_five {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_blod {
		font-size: 32rpx;
		font-family: DIN-Bold, DIN;
		font-weight: bold;
	}

	.text_d {
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_j {
		font-size: 34rpx;
		font-family: DIN Next LT Pro-Bold, DIN Next LT Pro;
		font-weight: bold;
	}

	.text_w {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
	}

	.text_x {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
	}

	.text_six {
		font-size: 26rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
	}

	.text_z {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 400;
	}

	.text_weight {
		font-size: 32rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;

	}

	.text_pee {
		font-size: 22rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_c {
		font-size: 24rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
	}

	.text_orange {
		font-size: 22rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #FD3850;
	}

	.text_e {
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_f {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.text_big {
		font-size: 68rpx;
		font-family: DIN Next LT Pro-Bold, DIN Next LT Pro;
		font-weight: bold;
	}

	.text_shi {
		font-size: 20rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.color_ling {
		color: #000000;
	}

	.color_san {
		color: #333333;
	}

	.color_six {
		color: #666666;
	}

	.color_jiu {
		color: #999999;
	}

	.radius {
		border-radius: 7rpx 7rpx 7rpx 7rpx;
	}

	.border {
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}

	.border_2 {
		border-radius: 14rpx 14rpx 14rpx 14rpx;
	}

	.border1 {
		border-radius: 11rpx 11rpx 11rpx 11rpx;
	}



	.gudingdb {
		padding: 30rpx 30rpx 60rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
	}

	.bigbtn {
		background-color: rgba(243, 39, 28, 1);
		border-radius: 41rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 82rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}
</style>